<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/23
 * Time: 15:10
 */

namespace console\controllers;


use common\models\c2\entity\ActivityModel;
use common\models\c2\entity\ActivityStaticsModel;
use common\models\c2\entity\DailyStaticsModel;
use common\models\c2\entity\OrderDailyStaticsModel;
use common\models\c2\entity\VoteRecordModel;
use common\models\c2\statics\OrderPayState;
use common\models\c2\statics\OrderPayType;
use cza\base\models\statics\EntityModelStatus;
use yii\console\Controller;
use yii\db\Exception;

class DailyStaticsController extends Controller
{

    /**
     * 每天晚上11点59分运行的当日活动数据
     */
    public function actionIndex()
    {
        $startDay = date("Y-m-d", time());
        $endDay = date("Y-m-d", strtotime('+1 day', time()));
        $orders = \common\models\c2\entity\GiftOrderModel::find()
            ->andFilterWhere(['state' => OrderPayState::PAY])
            ->andFilterWhere(['between', 'created_at', $startDay, $endDay])->all();
        $total_income = 0;
        foreach ($orders as $order) {
            $total_income += $order->pay_price;
        }
        $daily = new OrderDailyStaticsModel();
        $daily->total_income = $total_income;
        $daily->start_at = $startDay;
        $daily->end_at = $endDay;
        if ($daily->save()) {
            echo 1;
        } else {
            echo $daily->errors;
        }
    }

}